/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.gfx.menu.handlers;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import rpg.RPG;
import rpg.ui.components.Component;
import rpg.ui.components.ComponentState;
import rpg.ui.components.ComponentTextfield;
import rpg.ui.components.IClickableComponent;
import rpg.ui.components.ISubcomponentHolder;
import rpg.gfx.dialogs.Dialog;
import rpg.gfx.dialogs.DialogFactory;

/**
 * This is a handler that selects components based on mouse position. The user
 * won't see the "hover" animation in the menus using this handler.
 *
 * @author rothens
 */
public class SelectMenuHandler implements IMenuHandler {

    public int active;
    public ArrayList<Component> components;

    @Override
    public boolean handleTopEvents() {
        MouseEvent pos = RPG.mouselistener.lastPosition;
        int mouseX = (pos == null) ? -1 : pos.getX();
        int mouseY = (pos == null) ? -1 : pos.getY();

        boolean act = false;
        if (RPG.keylistener.isKeyDown2(KeyEvent.VK_TAB) || RPG.keylistener.isKeyDown2(KeyEvent.VK_DOWN)) {
            active = (active + 1) % components.size();
        }
        if (RPG.keylistener.isKeyDown2(KeyEvent.VK_UP)) {
            active--;
            if (active < 0) {
                active = components.size() - 1;
            }

        }
        Dialog d = DialogFactory.getInstance().activeDialog();
        if (d != null) {
            if (d.isMouseOver(mouseX, mouseY)) {
                d.handleEvents();
            }
        }
        for (int i = 0; i < components.size(); i++) {
            Component c = components.get(i);
            if (c.isMouseOver(mouseX, mouseY)) {
                act = true;
                c.setState(ComponentState.SELECTED);
                active = i;
            } else {
                c.setState(ComponentState.INACTIVE);
            }
            //c.redraw();
        }
        if (!act) {
            components.get(active).setState(ComponentState.SELECTED);
        }
        if (RPG.keylistener.isKeyDown(KeyEvent.VK_ENTER)) {
            components.get(active).action();
            return true;
        }
        if (RPG.mouselistener.isMouseClicked()) {
            for (Component c : components) {
                if (c instanceof IClickableComponent) {
                    if (((IClickableComponent) c).onRelease(mouseX, mouseY)) {
                        return true;
                    }
                }
            }
        }
        Component de = components.get(active);

        if (de instanceof ComponentTextfield) {
            ((ComponentTextfield) de).append(RPG.keylistener.getAndClearCache());
            if (RPG.keylistener.isKeyDown2(KeyEvent.VK_BACK_SPACE)) {
                ((ComponentTextfield) de).backspace();
            }
        } else {
            RPG.keylistener.clearCache();
        }
        return false;
    }

    @Override
    public int getNextFocusable(ArrayList<Component> components, int active) {
        for (int i = active + 1; i < active + components.size(); i++) {
            if (components.get(i % components.size()).isFocusable()) {
                return i % components.size();
            }
        }
        return active;
    }

    @Override
    public int getPrevFocusable(ArrayList<Component> components, int active) {
        int act = active;
        for (int i = active + 1; i < active + components.size(); i++) {
            if (components.get(i % components.size()).isFocusable()) {
                act = i % components.size();
            }
        }
        return act;
    }

    @Override
    public void onEnter(ArrayList<Component> components, int active) {
        components.get(active).action();
    }

    @Override
    public void setComponents(ArrayList<Component> components) {
        this.components = components;
    }

    @Override
    public boolean handleSubEvents(ArrayList<Component> components, ISubcomponentHolder parent, int x, int y) {
        return false;
    }
}
